from scipy import linalg, stats
from scipy.interpolate import interp1d
from scipy.optimize import curve_fit, minimize, root
import matplotlib.pyplot as plt
import numpy as np

arr = np.array([[1,2],
                [3,4]])

print(linalg.det(arr))
print(linalg.inv(arr))

measured_time = np.linspace(0,1,10)
noise = (np.random.random(10)*2 - 1) * 1e-1

measures = np.sin(2*np.pi * measured_time) + noise

linear_interp = interp1d(measured_time, measures)
cubic_interp = interp1d(measured_time, measures, kind='cubic')
interpolation_time = np.linspace(0, 1, 50)
linear_results = linear_interp(interpolation_time)
cubic_results = cubic_interp(interpolation_time)

plt.scatter(measured_time, measures,label='measures')

plt.plot(interpolation_time, cubic_results, 'r', label='cubic')
plt.plot(interpolation_time, linear_results, 'b', label='linear')
plt.legend()
plt.savefig('examplt_06.png')

plt.cla()



x_data = np.linspace(-5,5,50)
y_data = 2.9 * np.sin(1.5 * x_data) + np.random.normal(size=50)

def test_func(x, a, b):
    return a * np.sin(b * x)

params, params_covariance = curve_fit(test_func, x_data, y_data)

print(params)

plt.scatter(x_data, y_data,label='data')
plt.plot(x_data, test_func(x_data, *params), 'r', label='fit')

plt.legend()
plt.savefig('example_06_2.png')

plt.cla()

def f(x):
    return x**2 + 10*np.sin(x)

result = minimize(f,x0=0)
print(result)
print('---------------')
root = root(f, x0=1)
print(root.x)


bins = np.array([-3.5, -2.5, -1.5, -0.5, 0.5, 1.5, 2.5, 3.5])
pdf = stats.norm.pdf(bins)
print(pdf)

loc,std = stats.norm.fit(bins)
print(loc,std)

a = np.random.normal(0,1, size=100)
b = np.random.normal(1,1,size=10)
print(stats.ttest_ind(a,b))

exit()

t = np.linspace(0, 5, 100)
x = np.sin(t)

from scipy import signal

x_resampled = signal.resample(x, 25)
#print(x_resampled)

plt.plot(t,x)
plt.plot(t[::4], x_resampled, 'ko')
plt.savefig('example_06_3.png')

plt.cla()


x = t + np.random.normal(size=100)

x_detrended = signal.detrend(x)

plt.plot(t, x)
plt.plot(t, x_detrended)
plt.savefig('example_06_4.png')
plt.cla()

from scipy import fftpack

x_data = np.linspace(-5, 5, num=50)
y_data = 2.9 * np.sin(1.5 * x_data) + np.random.normal(size=50)

sig_fft = fftpack.fft(y_data)
#print(sig_fft)
freqs = fftpack.fftfreq(sig_fft.size, d=1.0 / 10)
print(freqs)
plt.plot(freqs, np.abs(sig_fft))
plt.savefig('example_06_5.png')